package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.CurriculumContentPlanVO;
import edu.zqu.instructionalMatters.project.entity.CurriculumContentPlan;
import edu.zqu.instructionalMatters.project.mapper.CurriculumContentPlanMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import edu.zqu.instructionalMatters.project.service.CourseService;
import edu.zqu.instructionalMatters.project.service.InstructionClassService;
import edu.zqu.instructionalMatters.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 课表计划授课内容 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "课表计划授课内容")
public class CurriculumContentPlanController {
    @Autowired
    private CurriculumContentPlanMapper curriculumContentPlanMapper;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserService userService;
    @Autowired
    private InstructionClassService instructionClassService;

    @ApiOperation(value = "获取课表计划授课内容列表")
    @RequestMapping(value = "/curriculumContentPlans", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "curriculumId", value = "课表id", paramType = "query", required = false),
            @ApiImplicitParam(name = "courseLearningScheduleId", value = "课程学习计划id", paramType = "query", required = false),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = false),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             BigInteger curriculumId,
                             BigInteger courseLearningScheduleId,
                             BigInteger courseId,
                             BigInteger teacherId,
                             BigInteger instructionClassId
    ) {
        CurriculumContentPlan curriculumContentPlan1 = new CurriculumContentPlan().setCollegeId(collegeId)
                .setCurriculumId(curriculumId).setCourseLearningScheduleId(courseLearningScheduleId)
                .setCourseId(courseId).setTeacherId(teacherId).setInstructionClassId(instructionClassId);               //CurriculumContentPlan 合成对象
        List<CurriculumContentPlan> list1 = curriculumContentPlanMapper.findAll(curriculumContentPlan1);                //获取 List<CurriculumContentPlan>
        List<CurriculumContentPlanVO> list = new ArrayList<>();                                                         //初始化 List<CurriculumContentPlanVO>
        for (CurriculumContentPlan curriculumContentPlan : list1) {                                                      //循环遍历
            CurriculumContentPlanVO curriculumContentPlanVO = new CurriculumContentPlanVO();                            //初始化 VO
            BeanUtils.copyProperties(curriculumContentPlan, curriculumContentPlanVO);                                    //对象复制赋值
            String collegeName = collegeService.getCollegeName(curriculumContentPlan.getCollegeId());                   //获取学校名称
            String courseName = courseService.getCourseName(curriculumContentPlan.getCourseId());                       //获取课程名称
            String teacherName = userService.getUserName(curriculumContentPlan.getTeacherId());                         //获取教师名称
            String instructionClassName = instructionClassService
                    .getInstructionClassName(curriculumContentPlan.getInstructionClassId());                            //获取授课班级名称
            curriculumContentPlanVO.setCollegeName(collegeName);                                                        //设置学校名称
            curriculumContentPlanVO.setCourseName(courseName);                                                          //设置课程名称
            curriculumContentPlanVO.setTeacherName(teacherName);                                                        //设置教师名称
            curriculumContentPlanVO.setInstructionClassName(instructionClassName);                                      //设置授课班级名称
            list.add(curriculumContentPlanVO);                                                                          //加入到 list
        }
        return ZQUResult.success(list);                                                                                 //成功返回 list
    }

    @ApiOperation(value = "获取课表计划授课内容信息", response = CurriculumContentPlanVO.class)
    @GetMapping(value = "/curriculumContentPlan/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        CurriculumContentPlan curriculumContentPlan = curriculumContentPlanMapper.selectById(id);        //获取课表计划授课内容对象
        String collegeName = collegeService.getCollegeName(curriculumContentPlan.getCollegeId());        //获取学校名称
        String courseName = courseService.getCourseName(curriculumContentPlan.getCourseId());            //获取课程名称
        String teacherName = userService.getUserName(curriculumContentPlan.getTeacherId());              //获取教师名称
        String instructionClassName = instructionClassService
                .getInstructionClassName(curriculumContentPlan.getInstructionClassId());                 //获取授课班级名称
        CurriculumContentPlanVO curriculumContentPlanVO = new CurriculumContentPlanVO();                 //初始化VO
        BeanUtils.copyProperties(curriculumContentPlan, curriculumContentPlanVO);                         //对象复制赋值
        curriculumContentPlanVO.setCollegeName(collegeName);                                             //设置学校名称
        curriculumContentPlanVO.setCourseName(courseName);                                               //设置课程名称
        curriculumContentPlanVO.setTeacherName(teacherName);                                             //设置教师名称
        curriculumContentPlanVO.setInstructionClassName(instructionClassName);                           //设置授课班级名称
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", curriculumContentPlanVO);              //成功返回对象数据
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/curriculumContentPlan", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "curriculumId", value = "课表id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseLearningScheduleId", value = "课程学习计划id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger curriculumId,
                         BigInteger courseLearningScheduleId,
                         BigInteger courseId,
                         BigInteger teacherId,
                         BigInteger instructionClassId,
                         BigInteger byWho) {
        CurriculumContentPlan curriculumContentPlan = new CurriculumContentPlan();               //初始化数据模型
        curriculumContentPlan.setCollegeId(collegeId);                                           //设置学校id
        curriculumContentPlan.setCurriculumId(curriculumId);                                     //设置课表id
        curriculumContentPlan.setCourseLearningScheduleId(courseLearningScheduleId);             //设置课程学习计划id
        curriculumContentPlan.setCourseId(courseId);                                             //设置课程id
        curriculumContentPlan.setTeacherId(teacherId);                                           //设置教师id
        curriculumContentPlan.setInstructionClassId(instructionClassId);                         //设置授课班级id
        curriculumContentPlan.setByWho(byWho);                                                   //设置操作者
        curriculumContentPlanMapper.insert(curriculumContentPlan);                               //执行插入
        return ZQUResult.success();                                                              //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/curriculumContentPlan/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "curriculumId", value = "课表id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseLearningScheduleId", value = "课程学习计划id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger curriculumId,
                            BigInteger courseLearningScheduleId,
                            BigInteger courseId,
                            BigInteger teacherId,
                            BigInteger instructionClassId,
                            BigInteger byWho) {
        CurriculumContentPlan curriculumContentPlan = curriculumContentPlanMapper.selectById(id);//查出旧记录
        curriculumContentPlan.setByWho(byWho);                                                   //修改操作者
        curriculumContentPlanMapper.updateById(curriculumContentPlan);                           //更新操作者
        curriculumContentPlanMapper.deleteById(id);                                              //逻辑删除
        curriculumContentPlan = new CurriculumContentPlan();                                     //初始化数据模型
        curriculumContentPlan.setCollegeId(collegeId);                                           //设置学校id
        curriculumContentPlan.setCurriculumId(curriculumId);                                     //设置课表id
        curriculumContentPlan.setCourseLearningScheduleId(courseLearningScheduleId);             //设置课程学习计划id
        curriculumContentPlan.setCourseId(courseId);                                             //设置课程id
        curriculumContentPlan.setTeacherId(teacherId);                                           //设置教师id
        curriculumContentPlan.setInstructionClassId(instructionClassId);                         //设置授课班级id
        curriculumContentPlan.setByWho(byWho);                                                   //设置操作者
        curriculumContentPlan.setLastVersion(id);                                                //设置上一版本
        curriculumContentPlanMapper.insert(curriculumContentPlan);                               //执行插入
        return ZQUResult.success();                                                              //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/curriculumContentPlan/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        CurriculumContentPlan curriculumContentPlan = curriculumContentPlanMapper.selectById(id);//查出旧记录
        curriculumContentPlan.setByWho(byWho);                                                   //修改操作者
        curriculumContentPlanMapper.updateById(curriculumContentPlan);                           //更新操作者
        curriculumContentPlanMapper.deleteById(id);                                              //逻辑删除
        return ZQUResult.success();                                                              //操作成功返回
    }
}

